﻿namespace MovieTicket.WebApi.Controllers
{
    using System.Net;
    using System.Net.Http;
    using System.Threading;
    using System.Web.Http;

    using MovieTicket.WebApi.BBL;
    using MovieTicket.WebApi.BBL.BusinessObjects;
    using MovieTicket.WebApi.Common;

    /// <summary>
    ///     Class AccountController
    /// </summary>
    [Authorize]
    public class AccountController : ApiController
    {
        #region Public Methods and Operators

        /// <summary>
        /// Gets this instance.
        /// </summary>
        /// <returns>UserBO.</returns>
        [Authorize]
        public UserBO Get()
        {
            // The user will be authenticated and Thread.CurrentPrincipal is set in the 
            // ValidateCredentials method of AuthMessagehandler class
            // before the control reaches this action method
            var currentPrincipal = Thread.CurrentPrincipal as MovieTicketsPrincipal;
            if (currentPrincipal != null)
            {
                return MovieTicketsBLL.GetLoggedInUserDetails(currentPrincipal.UserName);
            }

            return null;
        }

        /// <summary>
        /// Logins the specified username.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        [AllowAnonymous]
        public bool Login(string username, string password)
        {
            // any user name and password combination will work.
            return true;
        }

        /// <summary>
        /// Optionses this instance.
        /// </summary>
        /// <returns>HttpResponseMessage.</returns>
        [AllowAnonymous]
        public HttpResponseMessage Options()
        {
            var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
            return response;
        }

        /// <summary>
        /// Posts the specified updated user details.
        /// </summary>
        /// <param name="updatedUserDetails">The updated user details.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        [Authorize]
        public bool Post(UserBO updatedUserDetails)
        {
            // Code to update user details in the DB.
            // Since we are using hard coded values for the demo, no sae operation is done here.  
            return true;
        }

        #endregion
    }
}